/*
 * NCTTLP.c
 *
 *  Created on: 20/11/2012
 *      Author: gontijo
 */

#include "NewCattle.h"

void NCTTLP_Core_SM(unsigned char state){

    // Flag + Len + Query + ACK + Flag
    if(state == 0){
  	  TxBuffer[0] = NCTTLP_FLAG;
  	  TxBuffer[1] = 0x05;
  	  TxBuffer[2] = NCTTLP_QUERY;
  	  TxBuffer[3] = NCTTLP_QUERY_ACK;
  	  TxBuffer[4] = NCTTLP_FLAG;
    }

    // Flag + Len + Query + TID + Flag
    if(state == 1){
  	  TxBuffer[0] = NCTTLP_FLAG;
  	  TxBuffer[1] = 0x05;
  	  TxBuffer[2] = NCTTLP_QUERY;
  	  TxBuffer[3] = NCTTLP_QUERY_TID;
  	  TxBuffer[4] = NCTTLP_FLAG;
    }

    // Flag + Len + Query + RSSI + Flag
    if(state == 2){
  	  TxBuffer[0] = NCTTLP_FLAG;
  	  TxBuffer[1] = 0x05;
  	  TxBuffer[2] = NCTTLP_QUERY;
  	  TxBuffer[3] = NCTTLP_QUERY_RSSI;
  	  TxBuffer[4] = NCTTLP_FLAG;
    }

    // Flag + Len + Query + ACC + Flag
    if(state == 3){
  	  TxBuffer[0] = NCTTLP_FLAG;
  	  TxBuffer[1] = 0x05;
  	  TxBuffer[2] = NCTTLP_QUERY;
  	  TxBuffer[3] = NCTTLP_QUERY_ACC;
  	  TxBuffer[4] = NCTTLP_FLAG;
    }

    // Flag + Len + Query + SEARCH + Flag
    if(state == 4){
  	  TxBuffer[0] = NCTTLP_FLAG;
  	  TxBuffer[1] = 0x05;
  	  TxBuffer[2] = NCTTLP_QUERY;
  	  TxBuffer[3] = NCTTLP_QUERY_SEARCH;
  	  TxBuffer[4] = NCTTLP_FLAG;
    }

}

unsigned char TrataPkt(void ){

	unsigned char i=0, j=0, k=0, len=2;
	unsigned char ret=0x00;
	unsigned char msg[PACKET_LEN+2];
	unsigned char msg_2[PACKET_LEN+2];

	// Clean out the RX Buffer
	i = PACKET_LEN+2;
	while(i--)
	{
		msg[i] = 0;
	}

	// Busca pela 1 Flag
	for (i=0; i<(PACKET_LEN+2);i++){
		if (RxBuffer[i] == 0x7E){
			msg[k] = RxBuffer[i];
			k++;
			break;
		}
	}

	// Busca pela 2 Flag
	for (j=i; j<(PACKET_LEN+2);j++){
		if (RxBuffer[j+1] == 0x7E){
			msg[k] = RxBuffer[j+1];
			break;
		}
		msg[k] = RxBuffer[j+1];
		k++;
		len++;
	}

	// Verifica o campo Length
	if(len != msg[1]) return NCTTLP_ERRLEN;

	// Trata CMD
	switch(msg[2]){

		case (NCTTLP_QUERY):

				switch(msg[3]){

					case NCTTLP_QUERY_TID:

						// Flag + Len + DATA + DATA_TID + TagIdentification_Value + Flag
						TxBuffer[0] = NCTTLP_FLAG;
						TxBuffer[1] = 0x06;
						TxBuffer[2] = NCTTLP_DATA;
						TxBuffer[3] = NCTTLP_DATA_TID;
						TxBuffer[4] = NCTTLP_TID;
						TxBuffer[5] = NCTTLP_FLAG;
						__delay_cycles(2800);

						break;

					case NCTTLP_QUERY_ACK:

						// Flag + Len + DATA + DATA_ACK + ACK + Flag
						TxBuffer[0] = NCTTLP_FLAG;
						TxBuffer[1] = 0x05;
						TxBuffer[2] = NCTTLP_DATA;
						TxBuffer[3] = NCTTLP_DATA_ACK;
						TxBuffer[4] = NCTTLP_FLAG;

						__delay_cycles(2800);


						break;

					case NCTTLP_QUERY_RSSI:

						// Flag + Len + DATA + DATA_RSSI + RSSI_Value + Flag
						TxBuffer[0] = NCTTLP_FLAG;
						TxBuffer[1] = 0x06;
						TxBuffer[2] = NCTTLP_DATA;
						TxBuffer[3] = NCTTLP_DATA_RSSI;
						TxBuffer[4] = ReadSingleReg(RSSI);
						TxBuffer[5] = NCTTLP_FLAG;

						__delay_cycles(2800);


						break;

					case NCTTLP_QUERY_ACC:

						// Flag + Len + DATA + DATA_ACC + ACC_Value + Flag
						TxBuffer[0] = NCTTLP_FLAG;
						TxBuffer[1] = 0x06;
						TxBuffer[2] = NCTTLP_DATA;
						TxBuffer[3] = NCTTLP_DATA_ACC;
						TxBuffer[4] = 0x77;
						TxBuffer[5] = NCTTLP_FLAG;

						__delay_cycles(2800);


						break;

					case NCTTLP_QUERY_SEARCH:

						searching = 1;

						// Procura por TAGs no alcance
						// 1 - Confirma o recebimento do Comando devolvendo o TID
						// Flag + Len + DATA + DATA_ACK + Flag
						TxBuffer[0] = NCTTLP_FLAG;
						TxBuffer[1] = 0x06;
						TxBuffer[2] = NCTTLP_DATA;
						TxBuffer[3] = NCTTLP_DATA_SEARCH;
						TxBuffer[4] = NCTTLP_TID;
						TxBuffer[5] = NCTTLP_FLAG;

						__delay_cycles(2800);


						// 2 - Procura pelas TID no alcance
						// Flag + Len + DATA + DATA_ACK + Flag
						TxBuffer[0] = NCTTLP_FLAG;
						TxBuffer[1] = 0x05;
						TxBuffer[2] = NCTTLP_QUERY;
						TxBuffer[3] = NCTTLP_QUERY_TID;
						TxBuffer[4] = NCTTLP_FLAG;

						__delay_cycles(2800);


						break;


					default:
						ret = NCTTLP_ERRRECP;
						break;

				}

			break;

		case (NCTTLP_DATA):

				switch(msg[3]){

					case NCTTLP_DATA_ACK:
						ack_ok = 1;
						msg_2[0] = RxBuffer[3];
						msg_2[1] = msg_2[0];
						msg_2[1]++;
						break;

					case NCTTLP_DATA_TID:
						if(searching == 1){
							// Flag + Len + DATA + DATA_eTID + TID_Value_fromOtherTAG + Flag
							TxBuffer[0] = NCTTLP_FLAG;
							TxBuffer[1] = 0x06;
							TxBuffer[2] = NCTTLP_DATA;
							TxBuffer[3] = NCTTLP_DATA_eTID;
							TxBuffer[4] = RxBuffer[4];
							TxBuffer[5] = NCTTLP_FLAG;

							__delay_cycles(2800);

							searching = 0;
						}
						msg_2[0] = RxBuffer[4];
						msg_2[1] = msg_2[0];
						msg_2[1]++;
						break;

					case NCTTLP_DATA_RSSI:
						msg_2[0] = RxBuffer[4];
						msg_2[1] = msg_2[0];
						msg_2[1]++;
						break;

					case NCTTLP_DATA_ACC:
						msg_2[0] = RxBuffer[4];
						msg_2[1] = msg_2[0];
						msg_2[1]++;
						break;

					case NCTTLP_DATA_SEARCH:
						msg_2[0] = RxBuffer[4];
						msg_2[1] = msg_2[0];
						msg_2[1]++;
						break;

					default:
						break;
				}

			break;

		case (NCTTLP_DATA_eACK):
			break;

		default:
			ret = NCTTLP_ERRRECP;
			break;
	}

	return ret;
}


